<template>
  <a-drawer :title="title" width="40%" :visible="visible" :confirmLoading="loading" @close="() => {
    this.visible = false
  }
    ">
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" v-bind="layout">
        <a-form-model-item label="存货编码" prop="Id">
          <a-input v-model="entity.InventoryCode" autocomplete="off" placeholder="存货编码" />
        </a-form-model-item>
        <a-form-model-item label="存货名称" prop="code">
          <a-input v-model="entity.InventoryName" autocomplete="off" placeholder="存货名称" />
        </a-form-model-item>
        <a-form-model-item label="规格型号" prop="name">
          <a-input v-model="entity.SpecByBus" autocomplete="off" placeholder="业务规格型号" />
        </a-form-model-item>
        <a-form-model-item label="计价方式" prop="PriceType">
          <a-input v-model="entity.PriceType" autocomplete="off" placeholder="计价方式" />
        </a-form-model-item>
        <a-form-model-item label="所属类别" prop="Category">
          <a-input v-model="entity.Category" autocomplete="off" placeholder="所属类别" />
        </a-form-model-item>
        <a-form-model-item label="品牌" prop="Brand">
          <a-input v-model="entity.Brand" autocomplete="off" placeholder="品牌" />
        </a-form-model-item>
        <a-form-model-item label="计量单位" prop="specification">
          <a-input v-model="entity.UnitByBus" autocomplete="off" placeholder="计量单位" />
        </a-form-model-item>
        <a-form-model-item label="产地" prop="Producer">
            <a-input v-model="entity.Producer" autocomplete="off" placeholder="产地" />
          </a-form-model-item>
          <a-form-model-item label="参考成本" prop="ReferenceCost">
            <a-input v-model="entity.ReferenceCost" autocomplete="off" placeholder="参考成本" />
          </a-form-model-item>
          <a-form-model-item label="最新成本" prop="LastCost">
            <a-input v-model="entity.LastCost" autocomplete="off" placeholder="最新成本" />
          </a-form-model-item>
          <a-form-model-item label="建档日期" prop="MadeDate">
            <a-date-picker v-model="entity.MadeDate" value-format='YYYY-MM-DD'/>
          </a-form-model-item>
        <a-form-model-item label="财务单位" prop="TaxUnitPrice">
          <a-input v-model="entity.UnitByFin" autocomplete="off" placeholder="财务单位" />
        </a-form-model-item>
        <a-form-model-item label="财务名称" prop="IsTax">
          <a-input v-model="entity.NameByFin" autocomplete="off" placeholder="财务名称" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div :style="{
      position: 'absolute',
      right: 0,
      bottom: 0,
      width: '100%',
      borderTop: '1px solid #e9e9e9',
      padding: '4px 16px',
      background: '#fff',
      textAlign: 'right',
      zIndex: 1
    }">
      <a-button type="primary" @click="handleSave">确定</a-button>&nbsp;
      <a-button @click="close">取消</a-button>&nbsp;
    </div>
  </a-drawer>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      title: ''
    }
  },
  methods: {
    handleSave() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$http.post('/Cfg/cfg_compare_unit/SaveData', this.entity).then(res => {
            if (res.Success) {
              this.$message.success('保存成功!')
              this.visible = false
              this.parentObj.getDataList();
            }
          }).catch(err => {
            this.$message.error(err)
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })

    },
    close() {
      this.visible = false
      this.entity = {}
    },
    init() {
      this.visible = true
      this.entity = { Coefficient :0}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/Cfg/cfg_compare_unit/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/OrderManagement/Ap_CloseBill/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
